package com.lagou.sink;

import org.apache.flink.api.common.functions.MapFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.datastream.SingleOutputStreamOperator;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

import java.util.HashMap;
import java.util.Map;

public class FlinkToKuduDemo {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        DataStreamSource<UserInfo> data = env.fromElements(
                new UserInfo("001", "Jack", 18),
                new UserInfo("002", "Rose", 20),
                new UserInfo("003", "Cris", 22),
                new UserInfo("004", "Lily", 19),
                new UserInfo("005", "Lucy", 21),
                new UserInfo("006", "Json", 24)
        );

        SingleOutputStreamOperator<Map<String, Object>> maped = data.map(new MapFunction<UserInfo, Map<String, Object>>() {
            public Map<String, Object> map(UserInfo value) throws Exception {
                HashMap<String, Object> map = new HashMap<String, Object>();
                map.put("id", value.getId());
                map.put("name", value.getName());
                map.put("age", value.getAge());
                return map;
            }
        });

        String masterAddr = "zb26105";
        String tableName = "flinktable";
        maped.addSink(new MySinkToKudu(masterAddr,tableName));

        env.execute();


    }
}
